<?php
function get($key){
    if(isset($_GET[$key])){
        if(get_magic_quotes_gpc()){
            return stripcslashes($_GET[$key]);
        }
        else{
            return $_GET[$key];
        }
    }
    else{
        return NULL;
    }
}

function post($key){
    if(isset($_POST[$key])){
        if(get_magic_quotes_gpc()){
            return stripcslashes($_POST[$key]);
        }
        else{
            return $_POST[$key];
        }
    }
    else{
        return NULL;
    }
}

function request($key){
    if(isset($_REQUEST[$key])){
        if(get_magic_quotes_gpc()){
            return stripcslashes($_REQUEST[$key]);
        }
        else{
            return $_REQUEST[$key];
        }
    }
    else{
        return NULL;
    }
}

function cookie($key){
    if(isset($_COOKIE[$key])){
        if(get_magic_quotes_gpc()){
            return stripcslashes($_COOKIE[$key]);
        }
        else{
            return $_COOKIE[$key];
        }
    }
    else{
        return NULL;
    }
}

function parameter($str){
    if(get_magic_quotes_gpc()){
        return stripcslashes($str);
    }
    else{
        return $str;
    }
}

function addslashes_sql($str){
    return "'".addslashes($str)."'";
}

function is_numeric_int($value) {
    if(is_numeric($value)){
        if(is_int($value)){
            return TRUE;
        }
        else{
            if((string)((int)$value)===$value){
                return TRUE;
            }
            else{
                return FALSE;
            }
        }
    }
    else{
        return FALSE;
    }
}

function commonCut($string,$length,$append=false) {
    $string=strip_tags($string);
    if(strlen($string)<=$length)
    {
        return $string;
    }
    else
    {
        $i=0;

        while($i<$length)
        {
            $stringTMP=substr($string,$i,1);
            if(ord($stringTMP)>=224)
            {
                $stringTMP=substr($string,$i,3);
                $i=$i+3;
            }
            elseif(ord($stringTMP)>=192)
            {
                $stringTMP=substr($string,$i,2);
                $i=$i+2;
            }
            else
            {
                $i=$i+1;
            }
            $stringLast[] = $stringTMP;
        }

        $stringLast = implode('',$stringLast);
            $stringLast.='..';

        return $stringLast;
    }
//    $string=strip_tags($string);
//    $string=preg_replace('/\s+/','',$string);
//    return substr($string,0,$length).'..';
}

function commonTime($param) {
    $param=explode(' ',$param);
    return $param[0];
}

function commonPagerParameters($arguments){
    $temp='';
    foreach($arguments as $key=>$value){
        $temp.=($key.':'.urldecode($value)).'|';
    }
    return substr($temp,0,strlen($temp)-1);
}

function commonUrlParameters($arguments,$replaceArguments=NULL,$url=''){
    $temp='';
    //--
    if($replaceArguments&&gettype($replaceArguments)==='array'){
        foreach($replaceArguments as $key=>$value){
            if($value===NULL){
                unset($arguments[$key]);
            }
            else{
                $arguments[$key]=$value;
            }
        }
    }
    //--
    if(count($arguments)>0){
        foreach($arguments as $key=>$value){
            $temp.=($key.'='.urlencode($value).'&');
        }
        $temp=substr($temp,0,strlen($temp)-1);
        $url.='?'.$temp;
    }
    else{
        return basename($_SERVER['SCRIPT_NAME']);
    }
    //--
    return $url;
}

function commonSelectedParameters($arguments,$selectedParameters,$selectedValue){
    if(count(array_filter($selectedParameters))===0){ //检测出原来给出的selectedParameters的参数都是NULL， 这代表可能需要如果这些值都不存在于当前页面的参数中时，需要选中。
        $existCount=0;
        foreach($selectedParameters as $key=>$value){
            if(array_key_exists($key,$arguments)){
                $existCount++;
            }
        }
        if($existCount===0){
            return $selectedValue;
        }
    }
    else{
        $selectedCount=0;
        foreach($selectedParameters as $key=>$value){
            if(array_key_exists($key,$arguments)&&urldecode(trim($arguments[$key]))==urldecode(trim($value))){
                $selectedCount++;
            }
        }
        //--
        if($selectedCount===count(array_filter($selectedParameters))){
            return $selectedValue;
        }
    }
}

function currentFilename(){
    return basename(__FILE__);
}

function currentScriptName(){
    return basename($_SERVER['SCRIPT_NAME']);
}

function header_http_action_invaild_value(){
    header('HTTP/1.0 450 - ACTION VALUE IS INVALID!');
}

function header_http_action_none(){
    header('HTTP/1.0 451 - ACTION NONE!');
}

function header_http_invaild_value(){
    header('HTTP/1.0 452 - INVAILD VALUE!');
}

function header_http_invaild_method_post(){
    header('HTTP/1.0 453 - NEED POST!');
}

function header_http_invaild_method_get(){
    header('HTTP/1.0 453 - NEED GET!');
}

function jsAlert($str)
{
echo '<script type="text/javascript">alert("'.$str.'");</script>';
}

function jsLocation($url,$target='_self')
{
if($target=='_self')
    echo '<script type="text/javascript">window.location="'.$url.'";</script>';
else if($target=='_parent')
    echo '<script type="text/javascript">parent.window.location="'.$url.'";</script>';
else if($target=='_top')
    echo '<script type="text/javascript">window.top.location="'.$url.'";</script>';
}

function jsHistory($step)
{
echo '<script type="text/javascript">history.go('.$step.');</script>';
}

